import {
	dbName,
	Social
} from '@/libs/social.sqlite.js';

const tableName = "friends";

const Friends = {
	init: function() {
		let tableStructure =
			`
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			name VARCHAR(80) NOT NULL,
			age TINYINT NOT NULL,
			avatar_url TEXT NOT NULL,
			avatar_path TEXT NOT NULL,
			remarks TEXT
			`;
		plus.sqlite.executeSql({
			name: dbName,
			sql: `create table if not exists ${tableName}(${tableStructure})`,
			success(e) {
				console.log("Succeed to executeSql");
			},
			fail(e) {
				console.log('Failed to executeSql: ' + JSON.stringify(e))
			}
		});
	},
	add: function(name, age, url, path, remarks) {
		let insertSql =
			`insert into ${tableName}(name, age, avatar_url, avatar_path, remarks) values ("${name}","${age}","${url}","${path}","${remarks}")`;
		// console.log("Prepare sql:" + insertSql);
		plus.sqlite.executeSql({
			name: dbName,
			sql: insertSql,
			success(e) {
				console.log("Succeed to executeSql");
			},
			fail(e) {
				console.log('Failed to executeSql: ' + JSON.stringify(e))
			}
		});
	},
	list: function(callback) {
		plus.sqlite.selectSql({
			name: dbName,
			sql: `select * from ${tableName}`,
			success(data) {
				console.log("Succeed to selectSql");
				callback(data);
			},
			fail(e) {
				console.log('Failed to selectSql: ' + JSON.stringify(e))
			}
		});
	},
}

export {
	Friends
}